package com.xiaoshuidi.cloud.module.rooms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.rooms.enums.DeviceTypeEnum;
import com.xiaoshuidi.cloud.module.rooms.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomSdiotDeviceMappingMapper;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomSdiotPayModeMappingMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotDeviceMapping;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotPayModeMapping;
import com.xiaoshuidi.cloud.module.rooms.service.RoomSdiotDeviceMappingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class RoomSdiotDeviceMappingServiceImpl implements RoomSdiotDeviceMappingService {


    private final RoomSdiotDeviceMappingMapper roomSdiotDeviceMappingMapper;
    private final RoomSdiotPayModeMappingMapper roomSdiotPayModeMappingMapper;


    @Override
    public CommonResult<String> bindingDeviceRoom(RoomSdiotDeviceMapping roomSdiotDeviceMapping) {
        // todo @黄光辉 常量要定义 枚举或者常量类
        //根据设备编号校验是否绑定了其他设备
        QueryWrapper<RoomSdiotDeviceMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_third_no", roomSdiotDeviceMapping.getDeviceThirdNo());
        RoomSdiotDeviceMapping rsdm = roomSdiotDeviceMappingMapper.selectOne(queryWrapper);
        if (rsdm != null) {
            return CommonResult.error(ErrorCodeConstants.HOUSE_DEVICE_BINDING);
        }
        QueryWrapper<RoomSdiotPayModeMapping> qw = new QueryWrapper<>();
        qw.eq("room_house_id", roomSdiotDeviceMapping.getRoomHouseId()).eq("pay_mode", DeviceTypeEnum.PAY_MODE_BEHIND.getName());
        if (roomSdiotDeviceMapping.getDeviceType().equals("ammeter")) {
            qw.eq("fee_type", "电费");
        } else if (roomSdiotDeviceMapping.getDeviceType().equals("cold_watermeter")) {
            qw.eq("fee_type", "冷水费");
        } else if (roomSdiotDeviceMapping.getDeviceType().equals("hot_watermeter")) {
            qw.eq("fee_type", "热水费");
        }
        List<RoomSdiotPayModeMapping> list = roomSdiotPayModeMappingMapper.selectList(qw);
        if (CollectionUtil.isNotEmpty(list)) {
            return CommonResult.error(ErrorCodeConstants.HOUSE_DEVICE_CAN_NOT_BINDING);
        }
        roomSdiotDeviceMappingMapper.insert(roomSdiotDeviceMapping);
        return CommonResult.success("设备绑定成功");

    }

    @Override
    public CommonResult<String> unbindingDeviceRoom(String deviceThirdNo, Long roomId) {
        QueryWrapper<RoomSdiotDeviceMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_third_no", deviceThirdNo).eq("room_house_id", roomId);
        roomSdiotDeviceMappingMapper.delete(queryWrapper);
        return CommonResult.success("取消关联成功");
    }

    @Override
    public List<RoomSdiotDeviceMapping> getDeviceListByRoomId(Long roomId) {
        QueryWrapper<RoomSdiotDeviceMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_house_id", roomId)
                .last(" ORDER BY CASE WHEN device_type = 'doorlock' THEN 1 WHEN device_type = 'ammeter' THEN 2 WHEN device_type = 'cold_watermeter' THEN 3 WHEN device_type = 'hot_watermeter' THEN 4 ELSE 4 END asc");
        return roomSdiotDeviceMappingMapper.selectList(queryWrapper);
    }

    @Override
    public RoomSdiotDeviceMapping getRoomIdByDeviceNo(String deviceNo, String deviceType) {
        QueryWrapper<RoomSdiotDeviceMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_no", deviceNo);
        queryWrapper.eq("device_type", deviceType);
        return roomSdiotDeviceMappingMapper.selectOne(queryWrapper);
    }

    @Override
    public RoomSdiotDeviceMapping getRoomIdByDeviceNo(String deviceNo) {
        QueryWrapper<RoomSdiotDeviceMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_no", deviceNo);
        queryWrapper.eq("deleted",0);
        return roomSdiotDeviceMappingMapper.selectOne(queryWrapper);
    }

    @Override
    public RoomSdiotDeviceMapping getDeviceMapping(String deviceThirdNo) {
        QueryWrapper<RoomSdiotDeviceMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_third_no", deviceThirdNo);
        return roomSdiotDeviceMappingMapper.selectOne(queryWrapper);
    }


}
